<template>
  <view class="app-container">
    <u-cell-group>
      <u-cell-item
        bg-color="#fffffF"
        title="修改登录密码"
        @click="toAccountSecurity"
        icon="lock-opened-fill"
      ></u-cell-item>
      <u-cell-item
        bg-color="#fffffF"
        title="隐私协议"
        @click="toPrivacy"
        icon="question-circle-fill"
      ></u-cell-item>
    </u-cell-group>
    <!-- #ifdef H5 -->
    <!-- <u-cell-group>
      <u-cell-item
        bg-color="#fffffF"
        title="清空缓存"
        :value="cacheSize"
        @click="clearCache"
        icon="lock-opened-fill"
      ></u-cell-item>
    </u-cell-group> -->
    <!-- <u-cell-group>
      <u-cell-item
        bg-color="#fffffF"
        title="版本号"
        :value="'V' + version"
        :arrow="false"
        icon="lock-opened-fill"
      ></u-cell-item>
    </u-cell-group> -->
    <!-- #endif -->
    <u-cell-group>
      <u-cell-item
        bg-color="#fffffF"
        title="账号注销"
        icon="trash-fill"
        @click="handleClear"
      ></u-cell-item>
    </u-cell-group>
    <!-- 退出登录 -->
    <u-button
      type="primary"
      class="m-20"
      :custom-style="customStyle"
      @click="handleSignOut"
    >
      退出登录
    </u-button>
    <!-- 注销功能模态框 -->
    <u-modal
      v-model="show"
      :content="content"
      @confirm="handerConfirm"
      :show-cancel-button="true"
    ></u-modal>
  </view>
</template>

<script>
export default {
  data() {
    return {
      customStyle: {
        // 注意驼峰命名，并且值必须用引号包括，因为这是对象
        backgroundColor: '#9D60FF',
      },
      version: '',
      cacheSize: '',
      hasLogin: false,
      show: false,
      content: '您确定要注销此用户吗？',
    };
  },
  created() {
    // #ifndef H5
    this.getCacheSize();
    this.getVersion();
    // #endif
    if (uni.getStorageSync('token')) {
      this.hasLogin = true;
    }
  },
  methods: {
    toPrivacy() {
      this.$zx.router.push({
        path: '/pages/main/privacy/privacy',
      });
    },
    handleSignOut() {
      uni.setStorageSync('token', null);
      uni.reLaunch({
        url: '/pages/main/login/login',
      });
    },
    toAccountSecurity() {
      uni.navigateTo({
        url: '/pages/main/change-password/change-password',
      });
    },
    getVersion() {
      this.version = plus.runtime.version;
    },
    getCacheSize() {
      //计算缓存
      plus.cache.calculate((size) => {
        if (size === 0) {
          this.cacheSize = '0MB';
        } else if (size < 1024) {
          this.cacheSize = size + 'B';
        } else if (size < 1048576) {
          this.cacheSize = (size / 1024).toFixed(2) + 'KB';
        } else if (size < 1073741824) {
          this.cacheSize = (size / 1048576).toFixed(2) + 'MB';
        } else {
          this.cacheSize = (size / 1073741824).toFixed(2) + 'GB';
        }
      });
    },
    // 清空缓存
    clearCache() {
      const _self = this;
      if (plus.os.name === 'Android') {
        let main = plus.android.runtimeMainActivity();
        let sdRoot = main.getCacheDir();
        let files = plus.android.invoke(sdRoot, 'listFiles');
        for (let i = 0; i < files.length; i++) {
          let filePath = '' + files[i];
          plus.io.resolveLocalFileSystemURL(filePath, function (entry) {
            if (entry.isDirectory) {
              entry.removeRecursively(
                function (entry) {
                  uni.showToast({
                    title: '清除成功',
                  });
                  _self.getCacheSize();
                },
                (errMsg) => {},
                (errMsg) => {}
              );
            } else {
              entry.remove();
            }
          });
        }
      } else {
        //IOS
        plus.cache.clear(
          () => {
            uni.showToast({
              title: '清除成功',
            });
            _self.getCacheSize();
          },
          (errMsg) => {}
        );
      }
    },
    // 账号注销
    handleClear() {
      this.show = true;
    },
    // 确定账号注销
    handerConfirm() {
      console.log('确定----->', 123);
      // 注销成功后提示
      uni.showToast({
        title: '注销成功',
        duration: 1000,
      });
      // 延迟1秒跳转回登录页面
      setTimeout(() => {
        this.$u.route({
          url: '/pages/main/login/login',
        });
        // 调用注销接口实现注销
        this.$zx.user_api.logoutLogin().then((data) => {
          console.log('logoutLogin----->155', data);
        });
      }, 1000);
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background-color: #ededed;
}
</style>
